home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1996 #1 / Amiga Plus CD - 1996 - No. 1.iso / pd / grafik / jpeg_v6 / source / jpeg-6 / rdswitch.c < prev    next >
C/C++ Source or Header  |  1995-06-14  |  10KB  |  343 lines

  1. /*
  2.  * rdswitch.c
  3.  *
  4.  * Copyright (C) 1991-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to process some of cjpeg's more complicated
  9.  * command-line switches.  Switches processed here are:
  10.  *    -qtables file        Read quantization tables from text file
  11.  *    -scans file        Read scan script from text file
  12.  *    -qslots N[,N,...]    Set component quantization table selectors
  13.  *    -sample HxV[,HxV,...]    Set component sampling factors
  14.  */
  15.  
  16. #include "cdjpeg.h"        /* Common decls for cjpeg/djpeg applications */
  17. #include <ctype.h>        /* to declare isdigit(), isspace() */
  18.  
  19.  
  20. /* Hack: get access to jpeg_zigzag_order[] table in jutils.c.
  21.  * Since it's declared in jpegint.h, normally can't see it from an application.
  22.  */
  23. #ifdef NEED_SHORT_EXTERNAL_NAMES
  24. #define jpeg_zigzag_order    jZIGTable
  25. #endif
  26. extern const int jpeg_zigzag_order[];
  27.  
  28.  
  29. LOCAL int
  30. text_getc (FILE * file)
  31. /* Read next char, skipping over any comments (# to end of line) */
  32. /* A comment/newline sequence is returned as a newline */
  33. {
  34.   register int ch;
  35.   
  36.   ch = getc(file);
  37.   if (ch == '#') {
  38.     do {
  39.       ch = getc(file);
  40.     } while (ch != '\n' && ch != EOF);
  41.   }
  42.   return ch;
  43. }
  44.  
  45.  
  46. LOCAL boolean
  47. read_text_integer (FILE * file, long * result, int * termchar)
  48. /* Read an unsigned decimal integer from a file, store it in result */
  49. /* Reads one trailing character after the integer; returns it in termchar */
  50. {
  51.   register int ch;
  52.   register long val;
  53.   
  54.   /* Skip any leading whitespace, detect EOF */
  55.   do {
  56.     ch = text_getc(file);
  57.     if (ch == EOF) {
  58.       *termchar = ch;
  59.       return FALSE;
  60.     }
  61.   } while (isspace(ch));
  62.   
  63.   if (! isdigit(ch)) {
  64.     *termchar = ch;
  65.     return FALSE;
  66.   }
  67.  
  68.   val = ch - '0';
  69.   while ((ch = text_getc(file)) != EOF) {
  70.     if (! isdigit(ch))
  71.       break;
  72.     val *= 10;
  73.     val += ch - '0';
  74.   }
  75.   *result = val;
  76.   *termchar = ch;
  77.   return TRUE;
  78. }
  79.  
  80.  
  81. GLOBAL boolean
  82. read_quant_tables (j_compress_ptr cinfo, char * filename,
  83.            int scale_factor, boolean force_baseline)
  84. /* Read a set of quantization tables from the specified file.
  85.  * The file is plain ASCII text: decimal numbers with whitespace between.
  86.  * Comments preceded by '#' may be included in the file.
  87.  * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
  88.  * The tables are implicitly numbered 0,1,etc.
  89.  * NOTE: does not affect the qslots mapping, which will default to selecting
  90.  * table 0 for luminance (or primary) components, 1 for chrominance components.
  91.  * You must use -qslots if you want a different component->table mapping.
  92.  */
  93. {
  94.   FILE * fp;
  95.   int tblno, i, termchar;
  96.   long val;
  97.   unsigned int table[DCTSIZE2];
  98.  
  99.   if ((fp = fopen(filename, "r")) == NULL) {
  100.     fprintf(stderr, "Can't open table file %s\n", filename);
  101.     return FALSE;
  102.   }
  103.   tblno = 0;
  104.  
  105.   while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */
  106.     if (tblno >= NUM_QUANT_TBLS) {
  107.       fprintf(stderr, "Too many tables in file %s\n", filename);
  108.       fclose(fp);
  109.       return FALSE;
  110.     }
  111.     table[0] = (unsigned int) val;
  112.     for (i = 1; i < DCTSIZE2; i++) {
  113.       if (! read_text_integer(fp, &val, &termchar)) {
  114.     fprintf(stderr, "Invalid table data in file %s\n", filename);
  115.     fclose(fp);
  116.     return FALSE;
  117.       }
  118.       /* Convert from natural order in the file to zigzag table order. */
  119.       table[jpeg_zigzag_order[i]] = (unsigned int) val;
  120.     }
  121.     jpeg_add_quant_table(cinfo, tblno, table, scale_factor, force_baseline);
  122.     tblno++;
  123.   }
  124.  
  125.   if (termchar != EOF) {
  126.     fprintf(stderr, "Non-numeric data in file %s\n", filename);
  127.     fclose(fp);
  128.     return FALSE;
  129.   }
  130.  
  131.   fclose(fp);
  132.   return TRUE;
  133. }
  134.  
  135.  
  136. #ifdef C_MULTISCAN_FILES_SUPPORTED
  137.  
  138. LOCAL boolean
  139. read_scan_integer (FILE * file, long * result, int * termchar)
  140. /* Variant of read_text_integer that always looks for a non-space termchar;
  141.  * this simplifies parsing of punctuation in scan scripts.
  142.  */
  143. {
  144.   register int ch;
  145.  
  146.   if (! read_text_integer(file, result, termchar))
  147.     return FALSE;
  148.   ch = *termchar;
  149.   while (ch != EOF && isspace(ch))
  150.     ch = text_getc(file);
  151.   if (isdigit(ch)) {        /* oops, put it back */
  152.     if (ungetc(ch, file) == EOF)
  153.       return FALSE;
  154.     ch = ' ';
  155.   } else {
  156.     /* Any separators other than ';' and ':' are ignored;
  157.      * this allows user to insert commas, etc, if desired.
  158.      */
  159.     if (ch != EOF && ch != ';' && ch != ':')
  160.       ch = ' ';
  161.   }
  162.   *termchar = ch;
  163.   return TRUE;
  164. }
  165.  
  166.  
  167. GLOBAL boolean
  168. read_scan_script (j_compress_ptr cinfo, char * filename)
  169. /* Read a scan script from the specified text file.
  170.  * Each entry in the file defines one scan to be emitted.
  171.  * Entries are separated by semicolons ';'.
  172.  * An entry contains one to four component indexes,
  173.  * optionally followed by a colon ':' and four progressive-JPEG parameters.
  174.  * The component indexes denote which component(s) are to be transmitted
  175.  * in the current scan.  The first component has index 0.
  176.  * Sequential JPEG is used if the progressive-JPEG parameters are omitted.
  177.  * The file is free format text: any whitespace may appear between numbers
  178.  * and the ':' and ';' punctuation marks.  Also, other punctuation (such
  179.  * as commas or dashes) can be placed between numbers if desired.
  180.  * Comments preceded by '#' may be included in the file.
  181.  * Note: we do very little validity checking here;
  182.  * jcmaster.c will validate the script parameters.
  183.  */
  184. {
  185.   FILE * fp;
  186.   int scanno, ncomps, termchar;
  187.   long val;
  188.   jpeg_scan_info * scanptr;
  189. #define MAX_SCANS  100        /* quite arbitrary limit */
  190.   jpeg_scan_info scans[MAX_SCANS];
  191.  
  192.   if ((fp = fopen(filename, "r")) == NULL) {
  193.     fprintf(stderr, "Can't open scan definition file %s\n", filename);
  194.     return FALSE;
  195.   }
  196.   scanptr = scans;
  197.   scanno = 0;
  198.  
  199.   while (read_scan_integer(fp, &val, &termchar)) {
  200.     if (scanno >= MAX_SCANS) {
  201.       fprintf(stderr, "Too many scans defined in file %s\n", filename);
  202.       fclose(fp);
  203.       return FALSE;
  204.     }
  205.     scanptr->component_index[0] = (int) val;
  206.     ncomps = 1;
  207.     while (termchar == ' ') {
  208.       if (ncomps >= MAX_COMPS_IN_SCAN) {
  209.     fprintf(stderr, "Too many components in one scan in file %s\n",
  210.         filename);
  211.     fclose(fp);
  212.     return FALSE;
  213.       }
  214.       if (! read_scan_integer(fp, &val, &termchar))
  215.     goto bogus;
  216.       scanptr->component_index[ncomps] = (int) val;
  217.       ncomps++;
  218.     }
  219.     scanptr->comps_in_scan = ncomps;
  220.     if (termchar == ':') {
  221.       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  222.     goto bogus;
  223.       scanptr->Ss = (int) val;
  224.       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  225.     goto bogus;
  226.       scanptr->Se = (int) val;
  227.       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  228.     goto bogus;
  229.       scanptr->Ah = (int) val;
  230.       if (! read_scan_integer(fp, &val, &termchar))
  231.     goto bogus;
  232.       scanptr->Al = (int) val;
  233.     } else {
  234.       /* set non-progressive parameters */
  235.       scanptr->Ss = 0;
  236.       scanptr->Se = DCTSIZE2-1;
  237.       scanptr->Ah = 0;
  238.       scanptr->Al = 0;
  239.     }
  240.     if (termchar != ';' && termchar != EOF) {
  241. bogus:
  242.       fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
  243.       fclose(fp);
  244.       return FALSE;
  245.     }
  246.     scanptr++, scanno++;
  247.   }
  248.  
  249.   if (termchar != EOF) {
  250.     fprintf(stderr, "Non-numeric data in file %s\n", filename);
  251.     fclose(fp);
  252.     return FALSE;
  253.   }
  254.  
  255.   if (scanno > 0) {
  256.     /* Stash completed scan list in cinfo structure.
  257.      * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
  258.      * but if you want to compress multiple images you'd want JPOOL_PERMANENT.
  259.      */
  260.     scanptr = (jpeg_scan_info *)
  261.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  262.                   scanno * SIZEOF(jpeg_scan_info));
  263.     MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info));
  264.     cinfo->scan_info = scanptr;
  265.     cinfo->num_scans = scanno;
  266.   }
  267.  
  268.   fclose(fp);
  269.   return TRUE;
  270. }
  271.  
  272. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  273.  
  274.  
  275. GLOBAL boolean
  276. set_quant_slots (j_compress_ptr cinfo, char *arg)
  277. /* Process a quantization-table-selectors parameter string, of the form
  278.  *     N[,N,...]
  279.  * If there are more components than parameters, the last value is replicated.
  280.  */
  281. {
  282.   int val = 0;            /* default table # */
  283.   int ci;
  284.   char ch;
  285.  
  286.   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
  287.     if (*arg) {
  288.       ch = ',';            /* if not set by sscanf, will be ',' */
  289.       if (sscanf(arg, "%d%c", &val, &ch) < 1)
  290.     return FALSE;
  291.       if (ch != ',')        /* syntax check */
  292.     return FALSE;
  293.       if (val < 0 || val >= NUM_QUANT_TBLS) {
  294.     fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
  295.         NUM_QUANT_TBLS-1);
  296.     return FALSE;
  297.       }
  298.       cinfo->comp_info[ci].quant_tbl_no = val;
  299.       while (*arg && *arg++ != ',') /* advance to next segment of arg string */
  300.     ;
  301.     } else {
  302.       /* reached end of parameter, set remaining components to last table */
  303.       cinfo->comp_info[ci].quant_tbl_no = val;
  304.     }
  305.   }
  306.   return TRUE;
  307. }
  308.  
  309.  
  310. GLOBAL boolean
  311. set_sample_factors (j_compress_ptr cinfo, char *arg)
  312. /* Process a sample-factors parameter string, of the form
  313.  *     HxV[,HxV,...]
  314.  * If there are more components than parameters, "1x1" is assumed for the rest.
  315.  */
  316. {
  317.   int ci, val1, val2;
  318.   char ch1, ch2;
  319.  
  320.   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
  321.     if (*arg) {
  322.       ch2 = ',';        /* if not set by sscanf, will be ',' */
  323.       if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
  324.     return FALSE;
  325.       if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
  326.     return FALSE;
  327.       if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
  328.     fprintf(stderr, "JPEG sampling factors must be 1..4\n");
  329.     return FALSE;
  330.       }
  331.       cinfo->comp_info[ci].h_samp_factor = val1;
  332.       cinfo->comp_info[ci].v_samp_factor = val2;
  333.       while (*arg && *arg++ != ',') /* advance to next segment of arg string */
  334.     ;
  335.     } else {
  336.       /* reached end of parameter, set remaining components to 1x1 sampling */
  337.       cinfo->comp_info[ci].h_samp_factor = 1;
  338.       cinfo->comp_info[ci].v_samp_factor = 1;
  339.     }
  340.   }
  341.   return TRUE;
  342. }
  343.